/***************************************************************************
 *    
 *      Copyright (c) 2009,2010 KaiTrade LLC (registered in Delaware)
 *                     All Rights Reserved Worldwide
 *
 * STRICTLY PROPRIETARY and CONFIDENTIAL
 *
 * WARNING:  This file is the confidential property of KaiTrade LLC For
 * use only by those with the express written permission and license from
 * KaiTrade LLC.  Unauthorized reproduction, distribution, use or disclosure 
 * of this file or any program (or document) is prohibited. 
 * 
 ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;


namespace K2AlgoSet
{
    public class BarsOverUnder :   PatternMatchBase
    {
        
        /// <summary>
        /// Over under threashold for either over or under the curve
        /// </summary>
        double m_OverUnderThreashold = 0.0;

        /// <summary>
        /// Curve/Bar name
        /// </summary>
        private string m_CurveName = "";
        private double m_CurveValue = 0.0;
        private double m_OverCount = 0.0;
        private double m_UnderCount = 0.0;

        /// <summary>
        /// Bar field used
        /// </summary>
        private string m_BarFieldName = "";
        private double m_BarFieldValue = 0.0;

        

        public BarsOverUnder()
        {
            m_Name = "BarsOverUnder";
            m_Log.Info("BarsOverUnder");
            
            
            
            m_Output = new double[2];
            m_Output[0] = -1;
            m_Output[1] = -1;
        }

       



        protected override  void parseParameters()
        {
            try
            {
                string[] myParms = m_ParameterString.Split(',');
                if (myParms.Length < 3)
                {
                    m_Log.Error("Must specified curvenames and count threashold:" + m_ParameterString);
                    return;
                }
                m_CurveName = myParms[0];
                m_BarFieldName = myParms[1];
                m_OverUnderThreashold = double.Parse(myParms[2]);
 
            }
            catch (Exception myE)
            {
                m_Log.Error("getData", myE);
            }
        }

       

        private void getData()
        {
            try
            {
                if (m_TSSet.LastCompleteItem != null)
                {
                    m_CurveValue = m_TSSet.LastCompleteItem.GetCurveValue(m_CurveName);

                    m_BarFieldValue = m_TSSet.LastCompleteItem.GetBarValuebyName(m_BarFieldName);
                }
                
                
            }
            catch (Exception myE)
            {
                m_Log.Error("getData", myE);
            }
        }

        /// <summary>
        /// Detect matches in the set associated with the matcher - this will
        /// typically set/publish conditions
        /// </summary>
        public override void DetectMatches(bool isAdded)
        {
            try
            {
                if (isAdded)
                {
                    // Get data
                    getData();
                    // Detect a Val1 up through Val2 crossover
                    if (m_BarFieldValue > m_CurveValue)
                    {
                        m_OverCount++;
                        m_UnderCount = 0;
                        if (m_OverCount > m_OverUnderThreashold)
                        {
                            if (m_Output[0] != 1)
                            {
                                m_Output[0] = 1;
                                m_Output[1] = 0;
                                SetTriggerResults("DNCNT", false);
                                SetTriggerResults("UPCNT", true);
                                
                                string myInfo = string.Format("CrossDetect: SetResults: Pub {0} TriggerName {1} TriggerState {2} ", m_PublishName, m_Alias + "UPCNT", "true");
                                m_Log.Info(myInfo);
                            }
                        }
                    }
                    else if (m_BarFieldValue < m_CurveValue)
                    {
                        m_OverCount = 0;
                        m_UnderCount++;
                        if (m_UnderCount > m_OverUnderThreashold)
                        {
                            if (m_Output[1] != 1)
                            {
                                m_Output[0] = 0;
                                m_Output[1] = 1;
                                SetTriggerResults("UPCNT", false);
                                SetTriggerResults("DNCNT", true);
                                
                                string myInfo = string.Format("CrossDetect: SetResults: Pub {0} TriggerName {1} TriggerState {2} ", m_PublishName, m_Alias + "DNCNT", "true");
                                m_Log.Info(myInfo);
                            }
                        }
                    }
                }



            }
            catch (Exception myE)
            {
                m_Log.Error("DetectMatches", myE);
            }
        }

       
       

        /// <summary>
        /// Get a list of the trigger names the pattern matcher publishes - if any
        /// </summary>
        public override List<string> TriggerNames
        {
            get
            {
                List<string> myRet = new List<string>();
                myRet.Add(m_Alias + "UPCNT");
                myRet.Add(m_Alias + "DNCNT");
                return myRet;
            }
        }
    }
}
